package org.kaer.election.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;

import org.kaer.election.R;
import org.kaer.election.bean.Positions;
import org.kaer.election.utils.LogUtils;

import java.util.List;

/**
 * Created by lizj on 2016/7/7.
 */
public class PositionAdapter extends BaseAdapter {
    private Context mContext;
    private List<Positions>posList;
    private ViewHolder mHolder;

    public PositionAdapter(Context mContext, List<Positions> posList) {
        this.mContext = mContext;
        this.posList = posList;
        for (Positions item : posList) {
            LogUtils.i("PositionAdapter " + item.toString());
        }
    }

    @Override
    public int getCount() {
        return posList.size();
    }

    @Override
    public Object getItem(int position) {
        return posList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_position_item, null);
            mHolder = new ViewHolder();
            mHolder.btnName = (Button) convertView.findViewById(R.id.layout_posItem_btnPosName);
            mHolder.llBg = (LinearLayout) convertView.findViewById(R.id.layout_posItem_llBg);

            convertView.setTag(mHolder);
        } else {
            mHolder = (ViewHolder) convertView.getTag();
        }
        Positions info = posList.get(position);
        mHolder.btnName.setText(info.positionName);
        if (info.iSelected) {
            mHolder.llBg.setBackgroundResource(R.drawable.pos_sel_bg);
        } else {
            mHolder.llBg.setBackgroundResource(R.drawable.pos_unsel_bg);

        }
        return convertView;
    }

    public static class ViewHolder {
        private Button btnName;
        private LinearLayout llBg;

    }
}
